package com.diver.flinkdemo.service.impl;

import com.diver.flinkdemo.config.PropsConfig;
import com.diver.flinkdemo.entity.xxt.StudentCard;
import com.diver.flinkdemo.mapper.mysqlMapper.StudentCardMapper;
import com.diver.flinkdemo.service.IFamilyMobileService;
import com.diver.flinkdemo.service.IMoreSettingService;
import com.diver.flinkdemo.service.IRedisService;
import com.diver.flinkdemo.service.IStudentCardService;
import com.diver.flinkdemo.utils.CommonResource;
import com.diver.flinkdemo.utils.DateUtil;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

/**
 * 学生证 Service 实现
 * Created by Wen Jun on 2018/06/29.
 */
//@Service
 @Slf4j
public class StudentCardServiceImpl implements IStudentCardService {
     

//    @Autowired
    private StudentCardMapper mapper;
    private SqlSession session;

//    @Autowired
    protected PropsConfig propsConfig;
//    @Autowired
    IRedisService redisService = new RedisServiceImpl();
//    @Autowired
    private IFamilyMobileService familyMobileService = new FamilyMobileService();
//    @Autowired
    private IMoreSettingService moreSettingService = new MoreSettingService();

    @Override
    public void factoryStudentCard(String stuId) {
        session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
        mapper = session.getMapper(StudentCardMapper.class);
        StudentCard existCard = mapper.selectById(stuId);
        if (null != existCard){
            delSettingInfo(stuId);
            //修改免打扰状态
            if (existCard.getNoBotherStatus() != null && existCard.getNoBotherStatus() == 1) {
                existCard.setNoBotherStatus(0);
                existCard.setUpdateTime(DateUtil.getDateNow());
                mapper.updateById(existCard);
                log.info("修改免打扰状态{}", stuId);
            }
        }
    }

    //删除设置信息、白名单
    private void delSettingInfo(String stuId){
        //删除白名单号码
        familyMobileService.clearFamilyMobile(stuId);
        log.info("清除白名单号码{}",stuId);
        //删除设置信息
        moreSettingService.deleteSettings(stuId);
        redisService.remove(CommonResource.PRE_COMMAND_MODE + stuId);
        log.info("清除设置信息{}",stuId);
    }

    /**
     * 获取学生卡证信息， iccId
     *
     * @param
     * @return
     */
    @Override
    public StudentCard getStudentCardByImei(String imei) {
//        return studentCardMapper.getStudentCardByImei(imei);
        try {
            log.info("获取初始化好的mysqlSessionFactory-{}",DynamicDataSourceUtil.mysqlSessionFactory);
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(StudentCardMapper.class);
            return mapper.selectById(imei);
        } finally {
            endOfSession(session);
        }
    }

    public void endOfSession(SqlSession session) {
        //关闭sqlSession
        if (session != null) {
            log.info("session commit -> session close");
//            session.commit();
            session.close();
        }
    }
}
